// pages/explore/explore.js
const app = getApp()

Page({
  data: {
    // 内容类型
    contentTypes: [
      { id: "all", name: "全部" },
      { id: "poetry", name: "诗词" },
      { id: "idiom", name: "成语" },
      { id: "classics", name: "古籍" },
      { id: "ai", name: "AI创作" }
    ],
    currentType: "all",
    
    // 快速筛选
    quickFilters: [
      { id: "recent", name: "最近更新" },
      { id: "popular", name: "热门推荐" },
      { id: "favorites", name: "收藏最多" },
      { id: "random", name: "随机推荐" }
    ],
    currentQuickFilter: "recent",
    
    // 搜索相关
    searchText: "",
    searchFocused: false,
    
    // 所有内容数据
    allContent: [
      // 诗词
      {
        id: 101,
        title: "春季相关的经典诗句赏析",
        category: "诗词",
        description: "精选描写春天的经典诗句，感受古人对春的赞美",
        icon: "/images/icons/poetry.png",
        bgColor: "rgba(230, 57, 70, 0.15)",
        dynasty: "全朝代",
        theme: "季节",
        difficulty: "初级"
      },
      {
        id: 102,
        title: "唐诗宋词中的月亮意象",
        category: "诗词",
        description: "分析月亮在唐诗宋词中的象征意义和文化内涵",
        icon: "/images/icons/poetry.png",
        bgColor: "rgba(230, 57, 70, 0.15)",
        dynasty: "唐、宋",
        theme: "自然",
        difficulty: "中级"
      },
      {
        id: 103,
        title: "李白诗歌中的豪放风格",
        category: "诗词",
        description: "解析李白诗歌的艺术特色和豪放风格的形成",
        icon: "/images/icons/poetry.png",
        bgColor: "rgba(230, 57, 70, 0.15)",
        dynasty: "唐",
        theme: "人物",
        difficulty: "高级"
      },
      
      // 成语
      {
        id: 201,
        title: "容易用错的成语辨析",
        category: "成语",
        description: "详解日常使用中容易混淆的成语及其正确用法",
        icon: "/images/icons/idiom.png",
        bgColor: "rgba(29, 53, 87, 0.15)",
        dynasty: "全朝代",
        theme: "语言",
        difficulty: "初级"
      },
      {
        id: 202,
        title: "来自历史故事的成语",
        category: "成语",
        description: "探寻成语背后的历史故事和文化渊源",
        icon: "/images/icons/idiom.png",
        bgColor: "rgba(29, 53, 87, 0.15)",
        dynasty: "全朝代",
        theme: "历史",
        difficulty: "中级"
      },
      
      // 古籍
      {
        id: 301,
        title: "《论语》中的智慧名言解读",
        category: "古籍",
        description: "解读《论语》中的经典语句，领悟儒家智慧",
        icon: "/images/icons/classics.png",
        bgColor: "rgba(255, 183, 3, 0.15)",
        dynasty: "春秋",
        theme: "哲学",
        difficulty: "中级"
      },
      {
        id: 302,
        title: "《道德经》中的处世之道",
        category: "古籍",
        description: "解析《道德经》中的经典章节，探讨其现代意义",
        icon: "/images/icons/classics.png",
        bgColor: "rgba(255, 183, 3, 0.15)",
        dynasty: "春秋",
        theme: "哲学",
        difficulty: "高级"
      },
      
      // AI创作
      {
        id: 401,
        title: "AI生成的现代古诗",
        category: "AI创作",
        description: "由人工智能创作的具有古典韵味的现代诗歌",
        icon: "/images/icons/ai.png",
        bgColor: "rgba(168, 218, 220, 0.3)",
        dynasty: "现代",
        theme: "创新",
        difficulty: "初级"
      }
    ],
    
    // 筛选后的内容
    filteredContent: [],
    
    // 加载状态
    loading: false,
    
    // 筛选器状态
    showDynastyFilter: false,
    showThemeFilter: false,
    showDifficultyFilter: false,
    
    // 筛选条件
    selectedDynasty: "全部",
    selectedTheme: "全部",
    selectedDifficulty: "全部"
  },
  
  onLoad(options) {
    // 如果从首页分类进入，设置当前类型
    if (options.category) {
      this.setData({
        currentType: options.category
      })
    }
    
    // 如果是从搜索进入，自动聚焦搜索框
    if (options.search === "true") {
      this.setData({
        searchFocused: true
      })
    }
    
    // 初始筛选内容
    this.filterContent()
  },
  
  // 返回上一页
  navigateBack() {
    wx.navigateBack()
  },
  
  // 设置当前类型
  setCurrentType(e) {
    this.setData({
      currentType: e.currentTarget.dataset.type
    })
    this.filterContent()
  },
  
  // 设置快速筛选
  setQuickFilter(e) {
    this.setData({
      currentQuickFilter: e.currentTarget.dataset.id
    })
    this.filterContent()
  },
  
  // 搜索输入变化
  onSearchInput(e) {
    this.setData({
      searchText: e.detail.value
    })
  },
  
  // 执行搜索
  performSearch() {
    this.filterContent()
  },
  
  // 显示朝代筛选
  showDynastyFilter() {
    wx.showActionSheet({
      itemList: ["全部", "唐", "宋", "元", "明", "清", "其他"],
      success: (res) => {
        const dynasties = ["全部", "唐", "宋", "元", "明", "清", "其他"]
        this.setData({
          selectedDynasty: dynasties[res.tapIndex]
        })
        this.filterContent()
      }
    })
  },
  
  // 显示主题筛选
  showThemeFilter() {
    wx.showActionSheet({
      itemList: ["全部", "自然", "季节", "情感", "历史", "哲学", "人物", "语言", "创新"],
      success: (res) => {
        const themes = ["全部", "自然", "季节", "情感", "历史", "哲学", "人物", "语言", "创新"]
        this.setData({
          selectedTheme: themes[res.tapIndex]
        })
        this.filterContent()
      }
    })
  },
  
  // 显示难度筛选
  showDifficultyFilter() {
    wx.showActionSheet({
      itemList: ["全部", "初级", "中级", "高级"],
      success: (res) => {
        const difficulties = ["全部", "初级", "中级", "高级"]
        this.setData({
          selectedDifficulty: difficulties[res.tapIndex]
        })
        this.filterContent()
      }
    })
  },
  
  // 语音搜索
  showVoiceSearch() {
    wx.showToast({
      title: '语音搜索开发中',
      icon: 'none'
    })
  },
  
  // 图像搜索
  showCameraSearch() {
    wx.showToast({
      title: '图像搜索开发中',
      icon: 'none'
    })
  },
  
  // 筛选内容
  filterContent() {
    this.setData({
      loading: true
    })
    
    setTimeout(() => {
      let result = [...this.data.allContent]
      
      // 按类型筛选
      if (this.data.currentType !== "all") {
        // 转换类型名称以匹配category
        const typeMap = {
          "poetry": "诗词",
          "idiom": "成语",
          "classics": "古籍",
          "ai": "AI创作"
        }
        const category = typeMap[this.data.currentType] || this.data.currentType
        result = result.filter(item => item.category === category)
      }
      
      // 按搜索文本筛选
      if (this.data.searchText) {
        const searchText = this.data.searchText.toLowerCase()
        result = result.filter(item => 
          item.title.toLowerCase().includes(searchText) || 
          item.description.toLowerCase().includes(searchText) ||
          item.category.toLowerCase().includes(searchText)
        )
      }
      
      // 按朝代筛选
      if (this.data.selectedDynasty !== "全部") {
        result = result.filter(item => 
          item.dynasty.includes(this.data.selectedDynasty)
        )
      }
      
      // 按主题筛选
      if (this.data.selectedTheme !== "全部") {
        result = result.filter(item => item.theme === this.data.selectedTheme)
      }
      
      // 按难度筛选
      if (this.data.selectedDifficulty !== "全部") {
        result = result.filter(item => item.difficulty === this.data.selectedDifficulty)
      }
      
      // 按快速筛选条件排序
      if (this.data.currentQuickFilter === "popular") {
        // 模拟热门排序
        result.sort(() => Math.random() - 0.5)
      } else if (this.data.currentQuickFilter === "favorites") {
        // 模拟收藏最多排序
        result.sort(() => 0.5 - Math.random())
      } else if (this.data.currentQuickFilter === "random") {
        // 随机排序
        result.sort(() => Math.random() - 0.5)
      }
      // 默认按最近更新排序（原顺序）
      
      this.setData({
        filteredContent: result,
        loading: false
      })
    }, 500)
  },
  
  // 前往详情页
  goToDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/detail/detail?id=${id}`
    })
  }
})
    